package ServletController;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.dao.accDAO;
import model.domain.accDTO;
import model.service.documentBuilder;

public class Servlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	public Servlet() {
		super();
	}

	public void init(ServletConfig config) throws ServletException {

	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		try {
			process(request, response);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

	}

	protected void process(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException,SQLException {
		request.setCharacterEncoding("utf-8");
		
		System.out.println(1);
	//accDAO.createTable("lspt");
		System.out.println(2);
		
		
		String tagId = request.getParameter("service");
		String tagDate = request.getParameter("date");

		long tagIntDate = Long.parseLong(tagDate);
		
		List li = accDAO.selectTagId();
		List li2 = accDAO.selectRank();
		
		
		//((Cookie)li2).setPath("http://localhost:8080/DBClickCount/index.html");
	
//		for(int i=0; i< li2.size(); i++){
//			System.out.println(((accDTO)li2.get(i)).getTagId());
//		}
		
		
		
		//System.out.println(li.size());
		//System.out.println(li2);
		//System.out.println(((accDTO)li2.get(0)).getTagId());
		if (li.contains(tagId)) {
			accDAO.updateAccClick(new accDTO("lspt", tagId, tagIntDate, 1000, 0));
		} else {
			accDAO.insertAccClick(new accDTO("lspt", tagId, tagIntDate, 1, 0));
			for(int i=0; i<li.size(); i++){
				System.out.println("============"+((accDTO)li.get(i)).getTagId());
				}
		}
		
		documentBuilder.main(null);
	}
}
